package com.coin900.coin.service.fu;

import com.coin900.coin.base.CoinConstants;
import com.coin900.coin.base.FuConstants;
import com.coin900.coin.persist.fu.FuCapitalDetailVO;
import com.coin900.coin.persist.fu.FuCapitalWithdrawVO;
import com.coin900.coin.persist.fu.FuUserWalletVO;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.value.mybatis.IDaoRouter;
import com.value.mybatis.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 资产提现
 *
 * @author shui
 * @create 2017-12-18
 */
@Service
public class FuCapitalWithdrawService implements IFuCapitalWithdrawService {

    @Override
    public List<FuCapitalWithdrawVO> loadFuCapitalWithdraw(Map<String, Object> params) {
        return this.daoRouter.query("FuCapitalWithdraw.loadFuCapitalWithdraw", params);
    }

    @Override
    public List<FuCapitalWithdrawVO> loadFuCapitalWithdrawForPage(Pagination page) {
        return this.daoRouter.query("FuCapitalWithdraw.loadFuCapitalWithdrawForPage", page);
    }

    @Override
    public void insertFuCapitalWithdraw(FuCapitalWithdrawVO fuCapitalWithdrawVO) {
        this.daoRouter.insert("FuCapitalWithdraw.insertFuCapitalWithdraw", fuCapitalWithdrawVO);
    }

    @Override
    public void doCreateFuCapitalWithdraw(FuCapitalWithdrawVO fuCapitalWithdrawVO, FuUserWalletVO walletVO, FuCapitalDetailVO capitalDetailVO) {
        // 锁定提现虚拟币
        this.walletService.doWallectBlock(walletVO);
        // 新建一条充值记录
        this.insertFuCapitalWithdraw(fuCapitalWithdrawVO);

        capitalDetailVO.setTargetId(fuCapitalWithdrawVO.getId());
        this.detailService.insertFuCapitalDetail(capitalDetailVO);
    }

    @Override
    public void updateFuCapitalWithdraw(FuCapitalWithdrawVO fuCapitalWithdrawVO) {
        this.daoRouter.update("FuCapitalWithdraw.updateFuCapitalWithdraw", fuCapitalWithdrawVO);
    }

    @Override
    public void checkFuCapitalWithdraw(FuCapitalWithdrawVO fuCapitalWithdrawVO) {
        this.daoRouter.update("FuCapitalWithdraw.updateFuCapitalWithdraw", fuCapitalWithdrawVO);
        Integer checkStatus = fuCapitalWithdrawVO.getCheckStatus();
        Double balance = fuCapitalWithdrawVO.getBalance();

        /**
         * 审核状态 0未通过 1通过 2审核中
         *
         * 0 - 扣除 blocked，增加 balance，增加 available
         * 1 - 修改状态，等待提现操作
         */

        FuUserWalletVO fuUserWalletVO = new FuUserWalletVO();
        fuUserWalletVO.setUserId(fuCapitalWithdrawVO.getUserId());
        fuUserWalletVO.setCoinId(fuCapitalWithdrawVO.getCoinId());
        fuUserWalletVO.setModifiedBy(fuCapitalWithdrawVO.getModifiedBy());
        fuUserWalletVO.setModifiedDate(fuCapitalWithdrawVO.getModifiedDate());
        if (checkStatus == FuConstants.FU_CHECK_STATUS_FAIL) {
            // 0 - 扣除 blocked,增加 available
            fuUserWalletVO.setBlocked(balance);
            fuUserWalletVO.setAvailable(balance);
            this.walletService.doWithdrawFail(fuUserWalletVO);
        } else if (checkStatus == FuConstants.FU_CHECK_STATUS_SUCCESS) {
            // 1 - 修改状态，等待提现操作
            FuCapitalDetailVO detailVO = new FuCapitalDetailVO();
            detailVO.setType(2);
            detailVO.setTargetId(fuCapitalWithdrawVO.getId());
            detailVO.setTransferStatus(CoinConstants.COIN_CAPITAL_CONFIRM_WITHOUT);
            this.detailService.updateFuCapitalDetail(detailVO);
        }

    }

    @Override
    public void deleteFuCapitalWithdraw(FuCapitalWithdrawVO fuCapitalWithdrawVO) {
        this.daoRouter.update("FuCapitalWithdraw.deleteFuCapitalWithdraw", fuCapitalWithdrawVO);
    }

    @Autowired
    private IFuCapitalDetailService detailService;
    @Autowired
    private IFuUserWalletService walletService;
    @Autowired
    private IDaoRouter daoRouter;

}